/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadStatusProduto.java
 *
 * Created on 12/11/2011, 14:25:54
 */
package visao;

import fachada.FachadaBDR;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import statusProduto.StatusProduto;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;

/**
 *
 * @author Administrador
 */
public class CadStatusProduto extends javax.swing.JFrame {

    /** Creates new form CadStatusProduto */
    public CadStatusProduto() {
        initComponents();

        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/StatusProduto.gif")).getImage());
    }

    public CadStatusProduto(int idStatusProduto) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/StatusProduto.gif")).getImage());

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            StatusProduto objstatusProduto = objFachadaBDR.procurarStatusProduto(idStatusProduto);

            jTextField_idCodigo.setText(String.valueOf(objstatusProduto.getIdStatusProdutos()));
            jTextField_Descricao.setText(objstatusProduto.getDescricaoStatus());
            jCheckBox_Status.setSelected(objstatusProduto.getStatusProduto());


        } catch (CamposObrigatoriosException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);


        } catch (ErroAcessoRepositorioException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);


        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_Codigo = new javax.swing.JLabel();
        jLabel_Descricao = new javax.swing.JLabel();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jTextField_Descricao = new javax.swing.JTextField();
        jCheckBox_Status = new javax.swing.JCheckBox();
        jLabel_Mensagem = new javax.swing.JLabel();
        jTextField_idCodigo = new javax.swing.JTextField();

        setTitle("Status Produto");
        setResizable(false);

        jLabel_Codigo.setText("Codigo :");

        jLabel_Descricao.setText("Nome :");

        jButton_Ok.setText("Ok");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        jCheckBox_Status.setSelected(true);
        jCheckBox_Status.setText("Ativo");

        jTextField_idCodigo.setText("0");
        jTextField_idCodigo.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_Descricao, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel_Codigo, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField_Descricao, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(339, Short.MAX_VALUE)
                .addComponent(jCheckBox_Status)
                .addGap(55, 55, 55))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jCheckBox_Status)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Codigo)
                    .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Descricao)
                    .addComponent(jTextField_Descricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton_Ok)
                        .addComponent(jButton_Cancelar))
                    .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:

        int idStatusProduto = Integer.parseInt(jTextField_idCodigo.getText());
        String nome = jTextField_Descricao.getText();
        Boolean status = jCheckBox_Status.isSelected();

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            StatusProduto statusproduto = new StatusProduto(idStatusProduto, nome, status, null);

            if (idStatusProduto == 0) {


                jTextField_Descricao.setText("");
                jCheckBox_Status.setSelected(false);

                objFachadaBDR.cadastrarStatusProduto(statusproduto);

            } else {

                objFachadaBDR.atualizarStatusProduto(statusproduto);

            }

            String acao = (idStatusProduto == 0) ? "Cadastrado" : "Editado";
            jLabel_Mensagem.setText("Status Produto " + acao + " Com Sucesso!!!");
            jLabel_Mensagem.setForeground(Color.BLACK);


        } catch (CamposObrigatoriosException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        } catch (ErroAcessoRepositorioException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }



    }//GEN-LAST:event_jButton_OkActionPerformed

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:

        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {

            dispose();

        } else {
        }



    }//GEN-LAST:event_jButton_CancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadStatusProduto().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_Status;
    private javax.swing.JLabel jLabel_Codigo;
    private javax.swing.JLabel jLabel_Descricao;
    private javax.swing.JLabel jLabel_Mensagem;
    private javax.swing.JTextField jTextField_Descricao;
    private javax.swing.JTextField jTextField_idCodigo;
    // End of variables declaration//GEN-END:variables
}
